package com.share.wechat.fristpage.repository.dao;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * 对答题部分的操作放在这里的哈
 * 么么么哒
 * Created by er on 2018/1/16.
 */
@Repository

public class AnswerDao {
    private final Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private EntityManager entityManager;

    /**
     * 根据配置编码查询出后台配置的相关信息
     * @param parentCode 配置信息
     * @return 集合
     */
    public String getUrlByParentCode(String parentCode) {
        String defaultUrl = "http://www.baidu.com";
        String sql = "select key_code from web_config_system where parent_code =:parentCode";
        Query query = entityManager.createNativeQuery(sql);
        query.setParameter("parentCode", parentCode);
        List<String> list = query.getResultList();

        if (CollectionUtils.isEmpty(list)) {
            logger.warn("未查询到相关信息配置" + parentCode);
            logger.warn("请联系相关人员检查相关的配置信息");
            logger.warn("返回的地址信息为" + defaultUrl);
            return defaultUrl;
        }
        defaultUrl = list.get(0);

        logger.info("查询到的相关地址的配置信息为" + defaultUrl);
        logger.info("根据查询的相关的地址的信息为" + parentCode);

        return defaultUrl;
    }

    /**
     * 根据后台配置的信息选取出过滤的字体的语句
     * @param parentCode 一组配置
     * @return 一组配置
     */
    public List<String> getSkipWord(String parentCode) {
        String sql = "select key_code from web_config_system where parent_code =:parentCode";
        Query query = entityManager.createNativeQuery(sql);
        query.setParameter("parentCode", parentCode);
        List<String> list = query.getResultList();
        return list;
    }
}
